package com.clh.seckill.controller;

import com.clh.seckill.pojo.Goods;
import com.clh.seckill.pojo.User;
import com.clh.seckill.service.IGoodsService;
import com.clh.seckill.service.UserService;
import com.clh.seckill.vo.DetailVo;
import com.clh.seckill.vo.GoodsVo;
import com.clh.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author cailinhu
 * @Description TODO
 * @Date 2021-10-28 15:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/goods")
public class GoodController {
    @Autowired
    UserService userService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;
    /**
     * @CookieValue  是直接获取页面上的cookie值--springmvc真尼玛牛逼
     * @param
     * @param model
     * @param
     * @return
     */
    @RequestMapping(value = "/toList",produces ="text/html;charset=utf-8")
    public String toList(Model model,User user,HttpServletRequest request,HttpServletResponse response){
        model.addAttribute("user",user);
        List<GoodsVo> goods = goodsService.findGoods();
        model.addAttribute("goodsList", goods);
        return this.getRedisHtml(model,request,response,"goodsList","goodsList");
    }

    @RequestMapping(value = "/toDetail/{goodsId}",produces ="text/html;charset=utf-8")
    public String toDetail(Model model,@PathVariable Long goodsId,User user,HttpServletRequest request,HttpServletResponse response){
        model.addAttribute("user",user);
        GoodsVo goods = goodsService.findGoodsById(goodsId);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date nowDate = new Date();
        int secKillStatus=0;
        if (nowDate.after(endDate)){
            //秒杀已经结束
            secKillStatus=2;
        }else if (nowDate.after(startDate)&&nowDate.before(endDate)){
            secKillStatus=1;//正在秒杀
        }
        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("startTime",startDate.getTime());
        model.addAttribute("endTime",endDate.getTime());
        model.addAttribute("goods",goods);
        return this.getRedisHtml(model,request,response,"goodsDetail"+goodsId,"goodsDetail");
    }

    /**
     * 使用redis做页面缓存
     * @param model
     * @param request
     * @param response
     * @param key redisKey
     * @param template 最终去往的地方html
     * @return
     */
    private String getRedisHtml(Model model,HttpServletRequest request,HttpServletResponse response,String key,String template){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html=(String)valueOperations.get(key);
        if (!StringUtils.isEmpty(html)){
            return html;
        }
        //手动渲染thy
        WebContext webContext=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process(template, webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set(key,html,1, TimeUnit.MINUTES);
        }
        return html;
    }

    /**
     *
     * @param model
     * @param goodsId
     * @param user
     * @param request
     * @param response
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/toDetail2/{goodsId}")
    public RespBean toDetail2(Model model,@PathVariable Long goodsId,User user,HttpServletRequest request,HttpServletResponse response){
        GoodsVo goods = goodsService.findGoodsById(goodsId);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date nowDate = new Date();
        int secKillStatus=0;
        if (nowDate.after(endDate)){
            //秒杀已经结束
            secKillStatus=2;
        }else if (nowDate.after(startDate)&&nowDate.before(endDate)){
            secKillStatus=1;//正在秒杀
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goods);
        detailVo.setSecKillStatus(secKillStatus);
        detailVo.setStartTime(startDate.getTime());
        detailVo.setEndTime(endDate.getTime());
        return RespBean.success(detailVo);
    }

}
